package com.mathlearning.auth.controller;

import com.mathlearning.auth.common.ApiResponse;
import com.mathlearning.auth.dto.discussion.*;
import com.mathlearning.auth.service.DiscussionService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/discussions")
public class DiscussionController {

    @Autowired
    private DiscussionService discussionService;

    /**
     * 获取讨论列表
     */
    @GetMapping
    public ApiResponse<DiscussionListResponse> getDiscussionList(
            DiscussionListRequest request,
            HttpServletRequest httpRequest) {
        Long userId = (Long) httpRequest.getAttribute("userId");
        DiscussionListResponse response = discussionService.getDiscussionList(request, userId);
        return ApiResponse.success(response);
    }

    /**
     * 获取讨论详情
     */
    @GetMapping("/{id}")
    public ApiResponse<DiscussionResponse> getDiscussionDetail(
            @PathVariable Long id,
            HttpServletRequest httpRequest) {
        Long userId = (Long) httpRequest.getAttribute("userId");
        DiscussionResponse response = discussionService.getDiscussionDetail(id, userId);
        return ApiResponse.success(response);
    }

    /**
     * 发布讨论
     */
    @PostMapping
    public ApiResponse<DiscussionResponse> createDiscussion(
            @Valid @RequestBody CreateDiscussionRequest request,
            HttpServletRequest httpRequest) {
        Long userId = (Long) httpRequest.getAttribute("userId");
        DiscussionResponse response = discussionService.createDiscussion(request, userId);
        return ApiResponse.success("发布成功", response);
    }

    /**
     * 删除讨论
     */
    @DeleteMapping("/{id}")
    public ApiResponse<?> deleteDiscussion(
            @PathVariable Long id,
            HttpServletRequest httpRequest) {
        Long userId = (Long) httpRequest.getAttribute("userId");
        discussionService.deleteDiscussion(id, userId);
        return ApiResponse.success("删除成功");
    }

    /**
     * 点赞/取消点赞讨论
     */
    @PostMapping("/{id}/like")
    public ApiResponse<LikeResponse> likeDiscussion(
            @PathVariable Long id,
            HttpServletRequest httpRequest) {
        Long userId = (Long) httpRequest.getAttribute("userId");
        LikeResponse response = discussionService.likeDiscussion(id, userId);
        return ApiResponse.success("操作成功", response);
    }

    /**
     * 收藏/取消收藏讨论
     */
    @PostMapping("/{id}/favorite")
    public ApiResponse<FavoriteResponse> favoriteDiscussion(
            @PathVariable Long id,
            HttpServletRequest httpRequest) {
        Long userId = (Long) httpRequest.getAttribute("userId");
        FavoriteResponse response = discussionService.favoriteDiscussion(id, userId);
        return ApiResponse.success("操作成功", response);
    }

    /**
     * 获取热门标签
     */
    @GetMapping("/tags/hot")
    public ApiResponse<List<TagResponse>> getHotTags() {
        List<TagResponse> tags = discussionService.getHotTags();
        return ApiResponse.success(tags);
    }
}








